﻿using System;
using Microsoft.SharePoint.WebControls;
using TSB.FarmPowerShellWizard.ControlTemplates.TSB.FarmPowerShellWizard;
using TSB.FarmPowerShellWizard.Repository;

namespace TSB.FarmPowerShellWizard.Layouts.TSB.FarmPowerShellWizard
{
    public partial class PSW_SearchApplication : LayoutsPageBase
    {
        private string SelectedSearchApp = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            btnCreateApp.Click += btnCreateApp_Click;
            btnContentSources.Click += btnContentSources_Click;
            btnScopes.Click += btnScopes_Click;
            btnManagedProperties.Click += btnManagedProperties_Click;
            SelectedSearchApp = Request.QueryString["searchApp"] ?? string.Empty;
            ltSearchAppName.Text = SelectedSearchApp;
        }

        void btnManagedProperties_Click(object sender, EventArgs e)
        {
            var searchAppRepository = new SearchAppRepository();
            SetPowerShellText(searchAppRepository.GetManagedPropertiesScript(SelectedSearchApp));
        }

        void btnScopes_Click(object sender, EventArgs e)
        {
            var searchAppRepository = new SearchAppRepository();
            SetPowerShellText(searchAppRepository.GetScopesScript(SelectedSearchApp));
        }

        void btnContentSources_Click(object sender, EventArgs e)
        {
            var searchAppRepository = new SearchAppRepository();
            SetPowerShellText(searchAppRepository.GetContentSourcesScript());
        }

        void btnCreateApp_Click(object sender, EventArgs e)
        {
            var serviceAppRepository = new ServiceAppRepository();
            SetPowerShellText(serviceAppRepository.GetServiceAppScript(Request.QueryString["searchApp"]));
        }

        void SetPowerShellText(string script)
        {
            if (string.IsNullOrEmpty(script))
            {
                phScriptContainer.Visible = false;
                txtPowerShell.Text = string.Empty;
                return;
            }

            phScriptContainer.Visible = true;
            txtPowerShell.Text = script;
        }
    
    }
}
